package com.sx.pipeline.ui.message

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.MessageTypeAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActMessageCenterBinding
import com.sx.pipeline.repos.http.res.MessageTypeVo


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 消息中心
 */
@Route(path = RoutePath.Message.MESSAGE_CENTER)
class MessageCenterActivity : CYDataBindingBaseActivity<ActMessageCenterBinding, MessageVM>
    (R.layout.act_message_center), View.OnClickListener {

    @JvmField
    @Autowired(name = IntentKey.DATA)
    var noticeNum: Int? = 0

    private lateinit var mAdapter: MessageTypeAdapter

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }

    }

    override fun onResume() {
        super.onResume()
        // 获取消息类型
        mViewModel.getNoticeTypeList()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 消息类型列表
        setMessageTypeList()

        // 全部已读
        observe(mViewModel.messageReadEvent) {
            if (it == true) {
                mViewModel.getNoticeTypeList()
            }
        }
    }

    private fun setMessageTypeList() {
        mBinding.recyclerView.layoutManager = LinearLayoutManager(
            this, LinearLayoutManager.VERTICAL, false
        )
        mViewModel.messageTypeListEvent.observe(this) {
            if (it?.size == 4) {
                // 未读消息数量
                mBinding.tvMessageNum.text =
                    "未读消息：${it[0].noticeQty + it[1].noticeQty + it[2].noticeQty + it[3].noticeQty}"
            }
            if (!this::mAdapter.isInitialized) {
                mAdapter = MessageTypeAdapter(it as MutableList<MessageTypeVo>)
                mBinding.recyclerView.adapter = mAdapter
                mAdapter.setOnItemClickListener { adapter, view, position ->
                    val info = adapter.data[position] as MessageTypeVo
                    RouteManager.Message.intentToMessageList(info.code, info.name)
                }
            } else {
                mAdapter.setNewInstance(it as MutableList<MessageTypeVo>?)
            }
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //全部已读
            R.id.tv_read -> {
                mViewModel.getAllHaveRead()
            }
        }
    }


}